//-首页各种数据获取
const { mysql } = require('../../mysql.js');
module.exports = async (ctx) => {
  //轮播图数据
  const banner = await mysql('nideshop_ad').where({
    ad_position_id: 1
  }).select()
  //tab类型数据
  const channel = await mysql('nideshop_channel').select()
  //= 属性的名字与前端是一致的，如果ctx.body = { 'banner1': banner,'channel2':channel1 } 那么前端拿到的数据应该就是data.banner1 和data.channel1
  //商品列表
  const brandList = await mysql('nideshop_brand').where({ is_new: 1 }).orderBy('new_sort_order', 'asc').limit(4).select()
  //新品首发数据
  const newGoods = await mysql('nideshop_goods').whereIn('id', [1181000, 1135002, 1134030, 1134032]).andWhere('is_new', 1).select()
  //人气推荐
  const hotGoods = await mysql('nideshop_goods').column('id', 'name', 'list_pic_url', 'retail_price', 'goods_brief').where({ is_hot: 1 }).limit(5).select()
  //专题精选
  const topicList = await mysql('nideshop_topic').limit(3).select()
  //   **好物
  //todo 这块拿两个数据的需要再看看
  const categoryList = await mysql('nideshop_category').where({ parent_id: 0 }).select()
  const newCategoryList = []
  for (let index = 0; index < categoryList.length; index++) {
    let item = categoryList[index]
    let childCategoryIds = await mysql('nideshop_category').where({
      parent_id: item.id
    }).column('id').select()
    //变成数组的形式
    childCategoryIds = childCategoryIds.map((item) => {
      return item.id
    })
    //在商品中找到在childCategoryIds里的7条数据
    const categoryGoods = await mysql('nideshop_goods').column('id', 'name', 'list_pic_url', 'retail_price').whereIn('category_id', childCategoryIds).limit(7).select()
    newCategoryList.push({
      'id': item.id,
      'name': item.name,
      'goodsList': categoryGoods
    })
  }
  ctx.body = { 'banner': banner, 'channel': channel, 'brandList': brandList, 'newGoods': newGoods, 'hotGoods': hotGoods, 'topicList': topicList, 'newCategoryList': newCategoryList }
}

